const sm4 = require('sm-crypto').sm4


function objectToBase64String(obj) {
  return base64.encode(JSON.stringify(obj));
}

// const  base64String  =  objectToBase64String(obj);console.log(base64String);

function isEmptyString(str) {
  return !str || str.trim().length === 0;
}

// 判断是否是有效的邮箱地址
function isValidEmail(email) {
  const emailRegex = /^[\w-]+(\.[\w-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$/;
  return emailRegex.test(email);
}

// 格式化日期为字符串（yyyy-MM-dd）
function formatDateToString(date) {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
}

function dataAsYMDHMS(date) {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

function dataAsYMDHM(date) {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0'); 
  return `${year}-${month}-${day} ${hours}:${minutes}`;
}


// 生成随机整数（min <= result <= max）
function getRandomInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function encryptByMd5(str) {
  var md5 = require('md5');
  return md5(str)
}

// 导出方法
module.exports = {
  isEmptyString,
  isValidEmail,
  formatDateToString,
  dataAsYMDHMS,
  dataAsYMDHM,
  getRandomInteger,
  encryptByMd5
};